#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/4/16 11:29
# @Author  : simple0426
# @Email   : istyle.simple@gmail.com
# @File    : mysql_conn.py
# @Software: PyCharm
# @desc    : 使用阿里云模板创建【云监控】下的自定义报警规则

import cms_post, subprocess

def get_mysql_conn():
    df = subprocess.Popen(['netstat', '-ant'], stdout=subprocess.PIPE)
    output = df.communicate()[0]
    output_str = output.decode('utf-8')
    output_list = output_str.split('\n')
    count = 0
    for item in output_list:
        item_list = item.split()
        if item_list.__len__() != 0:
            if '172.17.134.3:3306' in item_list[3] and 'ESTABLISHED' in item_list[-1]:
                count += 1
    return count
conn = get_mysql_conn()

if __name__ == '__main__':
    cms_post.post("1348886531033002","Mysql_conn",conn,"Count","ecsinstanceId=i-2ze7qovdcua9q24thzdu")
